import common
import copy
import jaccard
from collections import OrderedDict
import datetime
import numpy as np

def remove_transport_routers(data, min_sequence, start_date, end_date):
    tempData = copy.deepcopy(data)
    vd = common.to_vector_data(tempData, start_date, end_date)
    for idx in xrange(len(vd) - 1, 0, -1):
        max_count = 0
        curr_count = 0
        for num in vd[idx]:
            if num == 1:
                curr_count = curr_count + 1
            else:
                if max_count < curr_count:
                    max_count = curr_count
                curr_count = 0
        if max_count < min_sequence:
#            print 'seq: ', tempData.keys()[idx]
            tempData.pop(tempData.keys()[idx], None)
    return tempData
                
def calc_jaccard_fatures(router_data, jaccard_data, delta):
    ret_val = []
    jaccard_arr = OrderedDict()
    
    for s_date in router_data:
        if jaccard_data.has_key(s_date[0]):
            jaccard_arr[s_date[0]] = jaccard_data[s_date[0]]
        else:
            for i in range(delta):
                if jaccard_data.has_key(s_date[0] + datetime.timedelta(minutes = i)):
                    jaccard_arr[s_date[0] + datetime.timedelta(minutes = i)] = jaccard_data[s_date[0] + datetime.timedelta(minutes = i)]
                    break
                elif jaccard_data.has_key(s_date[0] - datetime.timedelta(minutes = i)):
                    jaccard_arr[s_date[0] - datetime.timedelta(minutes = i)] = jaccard_data[s_date[0] - datetime.timedelta(minutes = i)]
                    break

        jaccard_diff = [j-i for i, j in zip(jaccard_arr.values()[:-1], jaccard_arr.values()[1:])]           
        
    if len(jaccard_arr.values()) > 0: 
        ret_val.append(np.mean(jaccard_arr.values()))
        ret_val.append(np.std(jaccard_arr.values()))
        ret_val.append(max(jaccard_arr.values()))
        ret_val.append(min(jaccard_arr.values()))
        ret_val.append(len(jaccard_arr))
    else:
        ret_val.append(0)
        ret_val.append(0)
        ret_val.append(0)
        ret_val.append(0)
        ret_val.append(0)
        
    if len(jaccard_diff) > 0:
        ret_val.append(np.mean(jaccard_diff))
        ret_val.append(np.std(jaccard_diff))
    else:
        ret_val.append(0)
        ret_val.append(0)
    
    
    return ret_val 
      
def remove_routers_by_jaccard(data, data_orig, threshold_std, threshold_max, delta):
    tempData = copy.deepcopy(data) 
    jaccard_data = jaccard.calculate_jaccard(data_orig, delta)
    for router in data:

        jaccard_arr = OrderedDict()
        for s_date in tempData[router]:
            if jaccard_data.has_key(s_date[0]):
                jaccard_arr[s_date[0]] = jaccard_data[s_date[0]]
            else:
                for i in range(delta):
                    if jaccard_data.has_key(s_date[0] + datetime.timedelta(minutes = i)):
                        jaccard_arr[s_date[0] + datetime.timedelta(minutes = i)] = jaccard_data[s_date[0] + datetime.timedelta(minutes = i)]
                        break
                    elif jaccard_data.has_key(s_date[0] - datetime.timedelta(minutes = i)):
                        jaccard_arr[s_date[0] - datetime.timedelta(minutes = i)] = jaccard_data[s_date[0] - datetime.timedelta(minutes = i)]
                        break

        jaccard_diff = [j-i for i, j in zip(jaccard_arr.values()[:-1], jaccard_arr.values()[1:])]           
        
        if router in [('00:0e:8c:bc:37:f0', 'CommuteNet'), ('00:0e:8c:bd:46:88', 'CommuteNet'), ('00:0e:8c:bd:48:d8', 'CommuteNet'), ('00:0e:8c:bd:46:e8', 'CommuteNet'), ('00:0e:8c:bd:87:e8', 'CommuteNet'), ('00:0e:8c:bd:86:78', 'CommuteNet')]:
            print router, np.mean(jaccard_diff), np.std(jaccard_diff)
            
        print router, np.mean(jaccard_diff), np.std(jaccard_diff)
        
        if np.isnan(np.mean(jaccard_diff) + np.std(jaccard_diff)) or (np.mean(jaccard_diff) < 0.15 and np.std(jaccard_diff) > 0.2) or (np.mean(jaccard_arr.values()) < 0.5):
            tempData.pop(router, None)
#        else: 
#            print router, np.mean(jaccard_diff) + np.std(jaccard_diff)
#            print router
        
        
#        if np.std(jaccard_arr.values()) > threshold_std and np.mean(jaccard_arr.values()) < 0.8 :
#            tempData.pop(router, None)
            
#        num = round(len(jaccard_arr) * 0.37)
            
#        if len(jaccard_arr) == 0 or max(jaccard_arr.values()) < threshold_max:
#            tempData.pop(router, None)
            
#        elif len(sorted(jaccard_arr)[0:-int(num)]) > 0:
#            if max(sorted(jaccard_arr.values())[0:-int(num)]) < threshold:
#                tempData.pop(router, None)
#            print 'jac: ', router
    return tempData   
    
def remove_routers_min_time(data, min_time):
    tempData = copy.deepcopy(data) 
    for key in tempData.keys():
        if len(tempData[key]) < min_time:
            tempData.pop(key, None)
    return tempData
    
def is_transportation_router(data, class_data, router):  
    dates = [item[0] for item in data[router]]
    flags = OrderedDict()
    for s_date in dates:
        flags[s_date] = False
        for idx in xrange(0, len(class_data.items()) - 1):
            item = class_data.items()[idx]
            next_item = class_data.items()[idx + 1]
            if item[1] == 'walk':
                if s_date >= item[0] and s_date < next_item[0]:
                    flags[s_date] = True
    return len(flags.values()) == sum(flags.values())
    
def calc_transportation_routers(data):
    trans_routers = []
    for router in data.keys():
        if is_transportation_router(data, router):
            trans_routers.append(router)
    print len(trans_routers)
    
def remove_transport_routers_window(data, min_window, window_length, start_date, end_date):
    tempData = copy.deepcopy(data)
    vd = common.to_vector_data(tempData, start_date, end_date)
    for idx in xrange(len(vd) - 1, 0, -1):
        max_count = 0
        curr_count = 0
        vd[idx, idx + window_length]
        for idxx in xrange(0, len(vd[idx]) - window_length):
            curr_count = sum(vd[idx][idxx : idxx + window_length])
            if curr_count > max_count:
                max_count = curr_count
#        print max_count
        if max_count < min_window:
#            print 'seq: ', tempData.keys()[idx]
            tempData.pop(tempData.keys()[idx], None)
    return tempData
    
def remove_transport_routers_train_bus(data):
    tempData = copy.deepcopy(data)
    train_router_names = ['CommuteNet', 'Bedrebustur']
    for router in tempData.keys():
        if router[1] in train_router_names:
            tempData.pop(router, None)
    return tempData